﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;

namespace PylonLiveView
{
    class Measurement
    {
        public HObject Measmt_image1;
        public HTuple size_str1;
        public HTuple size_str2;
        public HTuple size_str3;

        public void action(HObject xx2)
        {
            size_str1 = " ";
            size_str2 = " ";
            size_str3 = " ";


            // Stack for temporary objects 
            HObject[] OTemp = new HObject[20];

            // Local iconic variables 

            HObject ho_Image = null, ho_GrayImage = null, ho_ImageEmphasize = null;
            HObject ho_RegionROI = null, ho_RegionErosionROI = null, ho_ConnectedRegions = null;
            HObject ho_SelectedRegions2 = null, ho_RegionTrans = null, ho_ImageROI = null;
            HObject ho_RegionIC = null, ho_RegionDilationIC = null, ho_ConnectedRegionsIC = null;
            HObject ho_SelectedRegionsIC = null, ho_RectangleIC = null;
            HObject ho_ImageAffinTransROI = null, ho_RegionAffineTransIC = null;
            HObject ho_ImageReducedIC = null, ho_ImageAffinTransIC = null;
            HObject ho_RegionPartIC = null, ho_RectanglePartIC1 = null;
            HObject ho_RectanglePartIC2 = null, ho_EmptyObjectRect = null;
            HObject ho_ObjectSelectedRect = null, ho_RectangleLeads1 = null;
            HObject ho_RectangleLeads2 = null, ho_RectangleLeads3 = null;
            HObject ho_RectangleLeads4 = null, ho_RectangleLeads = null;
            HObject ho_ObjectSelected = null;

            // Local control variables 

            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_Ratio = new HTuple(), hv_Max_area = new HTuple();
            HTuple hv_WindowHandle = new HTuple(), hv_Row = new HTuple();
            HTuple hv_Column = new HTuple(), hv_Phi = new HTuple();
            HTuple hv_RectLength1 = new HTuple(), hv_RectLength2 = new HTuple();
            HTuple hv_HomMat2D = new HTuple(), hv_RowPartIC = new HTuple();
            HTuple hv_ColumnPartIC = new HTuple(), hv_Phi_PartIC = new HTuple();
            HTuple hv_Length1_PartIC = new HTuple(), hv_Length2_PartIC = new HTuple();
            HTuple hv_WidthIC = new HTuple(), hv_HeightIC = new HTuple();
            HTuple hv_MeasureHandlePos1 = new HTuple(), hv_MeasureHandlePos2 = new HTuple();
            HTuple hv_RowEdgePos1 = new HTuple(), hv_ColumnEdgePos1 = new HTuple();
            HTuple hv_AmplitudePos1 = new HTuple(), hv_DistancePos1 = new HTuple();
            HTuple hv_RowEdgePos2 = new HTuple(), hv_ColumnEdgePos2 = new HTuple();
            HTuple hv_AmplitudePos2 = new HTuple(), hv_DistancePos2 = new HTuple();
            HTuple hv_RowEdgePos = new HTuple(), hv_ColumnEdgePos = new HTuple();
            HTuple hv_DistancePos = new HTuple(), hv_DistancePos1Max = new HTuple();
            HTuple hv_DistancePos2Max = new HTuple(), hv_BodyRatio = new HTuple();
            HTuple hv_LeadValue1 = new HTuple(), hv_LeadValue2 = new HTuple();
            HTuple hv_LeadDivisionValue = new HTuple(), hv_DetectLeadsStri = new HTuple();
            HTuple hv_SortedDistance = new HTuple(), hv_IndicesDistance = new HTuple();
            HTuple hv_i1 = new HTuple(), hv_i2 = new HTuple(), hv_Sorted = new HTuple();
            HTuple hv_LeftPin = new HTuple(), hv_RightPin = new HTuple();
            HTuple hv_MeanPinLength = new HTuple(), hv_i = new HTuple();
            HTuple hv_string = new HTuple(), hv_RowMeasureRec = new HTuple();
            HTuple hv_ColuMeasureRec = new HTuple(), hv_Length1_Pairs = new HTuple();
            HTuple hv_Length2_Pairs = new HTuple(), hv_Length_Pairs = new HTuple();
            HTuple hv_RectWidth = new HTuple(), hv_MeasureHandlePairs1 = new HTuple();
            HTuple hv_RowEdgeFirst = new HTuple(), hv_ColumnEdgeFirst = new HTuple();
            HTuple hv_AmplitudeFirst = new HTuple(), hv_RowEdgeSecond = new HTuple();
            HTuple hv_ColumnEdgeSecond = new HTuple(), hv_AmplitudeSecond = new HTuple();
            HTuple hv_IntraDistance = new HTuple(), hv_InterDistance = new HTuple();
            HTuple hv_MeasureResult = new HTuple(), hv_CameraParameters = null;
            HTuple hv_CameraPose = null, hv_Column_World_Pos1 = null;
            HTuple hv_Row_World_Pos2 = null;
            HTuple hv_j = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_ImageEmphasize);
            HOperatorSet.GenEmptyObj(out ho_RegionROI);
            HOperatorSet.GenEmptyObj(out ho_RegionErosionROI);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_RegionTrans);
            HOperatorSet.GenEmptyObj(out ho_ImageROI);
            HOperatorSet.GenEmptyObj(out ho_RegionIC);
            HOperatorSet.GenEmptyObj(out ho_RegionDilationIC);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsIC);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsIC);
            HOperatorSet.GenEmptyObj(out ho_RectangleIC);
            HOperatorSet.GenEmptyObj(out ho_ImageAffinTransROI);
            HOperatorSet.GenEmptyObj(out ho_RegionAffineTransIC);
            HOperatorSet.GenEmptyObj(out ho_ImageReducedIC);
            HOperatorSet.GenEmptyObj(out ho_ImageAffinTransIC);
            HOperatorSet.GenEmptyObj(out ho_RegionPartIC);
            HOperatorSet.GenEmptyObj(out ho_RectanglePartIC1);
            HOperatorSet.GenEmptyObj(out ho_RectanglePartIC2);
            HOperatorSet.GenEmptyObj(out ho_EmptyObjectRect);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelectedRect);
            HOperatorSet.GenEmptyObj(out ho_RectangleLeads1);
            HOperatorSet.GenEmptyObj(out ho_RectangleLeads2);
            HOperatorSet.GenEmptyObj(out ho_RectangleLeads3);
            HOperatorSet.GenEmptyObj(out ho_RectangleLeads4);
            HOperatorSet.GenEmptyObj(out ho_RectangleLeads);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            try
            {

                //list_image_files ('图片', 'default', [], ImageFiles)
                //for ImageIndex := 1 to |ImageFiles| by 1
                // dev_update_window(...); only in hdevelop
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.CloseWindow(HDevWindowStack.Pop());
                }
                //镀金小芯片
                //read_image (Image, '图片/Image__2018-06-30__16-17-01')
                //上海交大大芯片
                ho_Image.Dispose();
                ho_Image = xx2;
                //HOperatorSet.ReadImage(out ho_Image, "C:/Users/于立昆/Desktop/image2/2");
                //QFP小芯片
                //read_image (Image, '图片/Image__2018-08-10__18-43-24')
                //标准件小芯片
                //read_image (Image, '图片/Image__2018-07-12__14-27-43')
                //镀金大芯片
                //read_image (Image, '图片/Image__2018-06-30__15-43-23')
                //无引脚
                //read_image (Image, '图片/Image__2018-08-10__14-21-55')
                //镀金小芯片，短轴方向有引脚
                //read_image (Image, '图片/Image__2018-08-10__14-32-34')

                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                hv_Ratio = 0.25;
                hv_Max_area = hv_Width * hv_Height;
                HOperatorSet.SetWindowAttr("background_color", "white");
                HOperatorSet.OpenWindow(0, 0, hv_Width * hv_Ratio, hv_Height * hv_Ratio, 0, "invisible", "", out hv_WindowHandle);
                HDevWindowStack.Push(hv_WindowHandle);
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
                }
                //搪锡前裁剪出被测芯片
                ho_GrayImage.Dispose();
                HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
                ho_ImageEmphasize.Dispose();
                HOperatorSet.Emphasize(ho_GrayImage, out ho_ImageEmphasize, 9, 9, 1);
                ho_RegionROI.Dispose();
                HOperatorSet.Threshold(ho_ImageEmphasize, out ho_RegionROI, 220, 255);
                ho_RegionErosionROI.Dispose();
                HOperatorSet.ErosionCircle(ho_RegionROI, out ho_RegionErosionROI, 25);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_RegionErosionROI, out ho_ConnectedRegions);
                ho_SelectedRegions2.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions2,
                    "max_area", 70);
                ho_RegionTrans.Dispose();
                HOperatorSet.ShapeTrans(ho_SelectedRegions2, out ho_RegionTrans, "convex");
                ho_ImageROI.Dispose();
                HOperatorSet.ReduceDomain(ho_ImageEmphasize, ho_RegionTrans, out ho_ImageROI
                    );


                ho_RegionIC.Dispose();
                HOperatorSet.Threshold(ho_ImageROI, out ho_RegionIC, 0, 240);
                ho_RegionDilationIC.Dispose();
                HOperatorSet.DilationRectangle1(ho_RegionIC, out ho_RegionDilationIC, 3,
                    3);
                ho_ConnectedRegionsIC.Dispose();
                HOperatorSet.Connection(ho_RegionDilationIC, out ho_ConnectedRegionsIC);

                ho_SelectedRegionsIC.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegionsIC, out ho_SelectedRegionsIC,
                    "max_area", 70);
                HOperatorSet.SmallestRectangle2(ho_SelectedRegionsIC, out hv_Row, out hv_Column,
                    out hv_Phi, out hv_RectLength1, out hv_RectLength2);
                ho_RectangleIC.Dispose();
                HOperatorSet.GenRectangle2(out ho_RectangleIC, hv_Row, hv_Column, hv_Phi,
                    hv_RectLength1, hv_RectLength2);
                ho_RegionDilationIC.Dispose();
                HOperatorSet.DilationRectangle1(ho_RectangleIC, out ho_RegionDilationIC,
                    50, 50);

                //仿射变换，将芯片转正
                HOperatorSet.VectorAngleToRigid(hv_Row, hv_Column, hv_Phi, hv_Row, hv_Column,
                    (new HTuple(0)).TupleRad(), out hv_HomMat2D);
                ho_ImageAffinTransROI.Dispose();
                HOperatorSet.AffineTransImage(ho_ImageROI, out ho_ImageAffinTransROI, hv_HomMat2D,
                    "constant", "false");
                ho_RegionAffineTransIC.Dispose();
                HOperatorSet.AffineTransRegion(ho_SelectedRegionsIC, out ho_RegionAffineTransIC,
                    hv_HomMat2D, "nearest_neighbor");

                HOperatorSet.SmallestRectangle2(ho_RegionAffineTransIC, out hv_RowPartIC,
                    out hv_ColumnPartIC, out hv_Phi_PartIC, out hv_Length1_PartIC, out hv_Length2_PartIC);

                ho_RectangleIC.Dispose();
                HOperatorSet.GenRectangle2(out ho_RectangleIC, hv_RowPartIC, hv_ColumnPartIC,
                    hv_Phi_PartIC, hv_Length1_PartIC, hv_Length2_PartIC);
                ho_RegionDilationIC.Dispose();
                HOperatorSet.DilationRectangle1(ho_RectangleIC, out ho_RegionDilationIC,
                    50, 50);

                ho_ImageReducedIC.Dispose();
                HOperatorSet.ReduceDomain(ho_ImageAffinTransROI, ho_RegionDilationIC, out ho_ImageReducedIC
                    );
                ho_ImageAffinTransIC.Dispose();
                HOperatorSet.CropDomain(ho_ImageReducedIC, out ho_ImageAffinTransIC);
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_ImageAffinTransIC, HDevWindowStack.GetActive());
                }
                HOperatorSet.GetImageSize(ho_ImageAffinTransIC, out hv_WidthIC, out hv_HeightIC);


                Measmt_image1 = ho_ImageAffinTransIC;
                //threshold (ImageROI, RegionIC, 0, 240)
                //*     dilation_rectangle1 (RegionIC, RegionDilationIC, 3, 3)
                //connection (RegionDilationIC, ConnectedRegionsIC)

                //*     select_shape_std (ConnectedRegionsIC, SelectedRegionsIC, 'max_area', 70)
                //*     smallest_rectangle2 (SelectedRegionsIC, Row, Column, Phi, RectLength1, RectLength2)
                //*     gen_rectangle2 (RectangleIC, Row, Column, Phi, RectLength1, RectLength2)
                //dilation_rectangle1 (RectangleIC, RegionDilationIC, 50, 50)

                //reduce_domain (ImageROI, RegionDilationIC, ImageReducedIC)
                //crop_domain (ImageReducedIC, ImagePartIC)
                if (HDevWindowStack.IsOpen())
                {
                    //dev_clear_window ()
                }
                //*     dev_display (ImagePartIC)
                //*     get_image_size (ImagePartIC, WidthIC, HeightIC)

                //对裁剪出的芯片处理-只画出芯片区域
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin");
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                }
                ho_RegionPartIC.Dispose();
                HOperatorSet.Threshold(ho_ImageAffinTransIC, out ho_RegionPartIC, 0, 200);
                HOperatorSet.SmallestRectangle2(ho_RegionPartIC, out hv_RowPartIC, out hv_ColumnPartIC,
                    out hv_Phi_PartIC, out hv_Length1_PartIC, out hv_Length2_PartIC);

                //仿射变换
                //vector_angle_to_rigid (RowPartIC, ColumnPartIC, Phi_PartIC, RowPartIC, ColumnPartIC, rad(0), HomMat2D)
                //*     affine_trans_image (ImagePartIC, ImageAffinTrans, HomMat2D, 'constant', 'false')
                //affine_trans_region (RegionPartIC, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
                //*     smallest_rectangle2 (RegionAffineTrans, RowPartIC, ColumnPartIC, Phi_PartIC, Length1_PartIC, Length2_PartIC)
                //获得测量矩形
                ho_RectanglePartIC1.Dispose();
                HOperatorSet.GenRectangle2(out ho_RectanglePartIC1, hv_RowPartIC, hv_ColumnPartIC,
                    0, hv_Length1_PartIC + 8, hv_Length2_PartIC * 0.5);
                ho_RectanglePartIC2.Dispose();
                HOperatorSet.GenRectangle2(out ho_RectanglePartIC2, hv_RowPartIC, hv_ColumnPartIC,
                    (new HTuple(90)).TupleRad(), hv_Length2_PartIC + 8, hv_Length2_PartIC * 0.5);
                //合并2个矩形
                ho_EmptyObjectRect.Dispose();
                HOperatorSet.GenEmptyObj(out ho_EmptyObjectRect);
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.ConcatObj(ho_EmptyObjectRect, ho_RectanglePartIC1, out ExpTmpOutVar_0
                        );
                    ho_EmptyObjectRect.Dispose();
                    ho_EmptyObjectRect = ExpTmpOutVar_0;
                }
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.ConcatObj(ho_EmptyObjectRect, ho_RectanglePartIC2, out ExpTmpOutVar_0
                        );
                    ho_EmptyObjectRect.Dispose();
                    ho_EmptyObjectRect = ExpTmpOutVar_0;
                }

                //获得测量句柄
                HOperatorSet.GenMeasureRectangle2(hv_RowPartIC, hv_ColumnPartIC, 0, hv_Length1_PartIC + 8,
                    hv_Length2_PartIC * 0.5, hv_WidthIC, hv_HeightIC, "nearest_neighbor", out hv_MeasureHandlePos1);
                HOperatorSet.GenMeasureRectangle2(hv_RowPartIC, hv_ColumnPartIC, (new HTuple(90)).TupleRad()
                    , hv_Length2_PartIC + 8, hv_Length2_PartIC * 0.5, hv_WidthIC, hv_HeightIC,
                    "nearest_neighbor", out hv_MeasureHandlePos2);
                //测量引脚长度
                //镀金芯片
                HOperatorSet.MeasurePos(ho_ImageAffinTransIC, hv_MeasureHandlePos1, 1, 20,
                    "all", "all", out hv_RowEdgePos1, out hv_ColumnEdgePos1, out hv_AmplitudePos1,
                    out hv_DistancePos1);
                HOperatorSet.MeasurePos(ho_ImageAffinTransIC, hv_MeasureHandlePos2, 1, 20,
                    "all", "all", out hv_RowEdgePos2, out hv_ColumnEdgePos2, out hv_AmplitudePos2,
                    out hv_DistancePos2);
                //非镀金芯片
                //measure_pos (ImageAffinTrans, MeasureHandle, 1, 40, 'all', 'all', RowEdge, ColumnEdge, Amplitude, Distance)
                HOperatorSet.CloseMeasure(hv_MeasureHandlePos1);
                HOperatorSet.CloseMeasure(hv_MeasureHandlePos2);
                //显示测量结果
                //定义空的数组
                hv_RowEdgePos = new HTuple();
                hv_ColumnEdgePos = new HTuple();
                hv_DistancePos = new HTuple();


                //判断是否检测到引脚，并判断哪个方向有引脚
                //分别求0、90度角方向芯片本体尺寸
                HOperatorSet.TupleMax(hv_DistancePos1, out hv_DistancePos1Max);
                HOperatorSet.TupleMax(hv_DistancePos2, out hv_DistancePos2Max);
                hv_BodyRatio = hv_DistancePos1Max / hv_DistancePos2Max;
                //外接矩形的长轴、短轴分别减去0、90度角方向芯片本体尺寸
                hv_LeadValue1 = (hv_Length1_PartIC * 2) - hv_DistancePos1Max;
                hv_LeadValue2 = ((((hv_Length2_PartIC * 2) - hv_DistancePos2Max)).TupleAbs()) + 0.0001;
                //差值的比值
                hv_LeadDivisionValue = hv_LeadValue1 / hv_LeadValue2;
                //判断那边有引脚

                hv_DetectLeadsStri = new HTuple();
                hv_DetectLeadsStri[0] = "器件无引脚";
                hv_DetectLeadsStri[1] = "正方形四边有引脚";
                hv_DetectLeadsStri[2] = "长方形四边有引脚";
                hv_DetectLeadsStri[3] = "长轴方向两边有引脚";
                hv_DetectLeadsStri[4] = "短轴方向两边有引脚";
                //器件无引脚
                if ((int)((new HTuple(hv_LeadValue1.TupleLess(20))).TupleAnd(new HTuple(hv_LeadValue2.TupleLess(
                    20)))) != 0)
                {
                    //set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_ImageAffinTransIC, HDevWindowStack.GetActive()
                            );
                    }
                    size_str1 = hv_DetectLeadsStri.TupleSelect(0);
                    //disp_message (200000, DetectLeadsStri[0], 'ImageAffinTrans', RowPartIC, ColumnPartIC, 'red', 'false')
                    // stop(...); only in hdevelop
                    //disp_continue_button (WindowHandle)
                    //长轴方向有引脚
                }
                else if ((int)(new HTuple(hv_LeadDivisionValue.TupleGreater(10))) != 0)
                {
                    ho_ObjectSelectedRect.Dispose();
                    HOperatorSet.SelectObj(ho_EmptyObjectRect, out ho_ObjectSelectedRect, 1);
                    hv_RowEdgePos = hv_RowEdgePos1.Clone();
                    hv_ColumnEdgePos = hv_ColumnEdgePos1.Clone();
                    hv_DistancePos = hv_DistancePos1.Clone();
                    //set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_ImageAffinTransIC, HDevWindowStack.GetActive()
                            );
                    }
                    size_str1 = hv_DetectLeadsStri.TupleSelect(3);
                    //disp_message (200000, DetectLeadsStri[3], 'ImageAffinTrans', RowPartIC-DistancePos2Max*0.35, ColumnPartIC, 'green', 'false')
                    //绘制长度边界
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                    }
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_ObjectSelectedRect, HDevWindowStack.GetActive()
                            );
                    }
                    HOperatorSet.DispLine(200000, hv_RowEdgePos - 50, hv_ColumnEdgePos, hv_RowEdgePos + 50,
                        hv_ColumnEdgePos);

                    //短轴方向有引脚
                }
                else if ((int)(new HTuple(hv_LeadDivisionValue.TupleLess(0.95))) != 0)
                {
                    ho_ObjectSelectedRect.Dispose();
                    HOperatorSet.SelectObj(ho_EmptyObjectRect, out ho_ObjectSelectedRect, 2);
                    hv_RowEdgePos = hv_RowEdgePos2.Clone();
                    hv_ColumnEdgePos = hv_ColumnEdgePos2.Clone();
                    hv_DistancePos = hv_DistancePos2.Clone();
                    //set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_ImageAffinTransIC, HDevWindowStack.GetActive()
                            );
                    }
                    //disp_message (200000, DetectLeadsStri[4], 'ImageAffinTrans', RowPartIC-DistancePos2Max*0.35, ColumnPartIC, 'green', 'false')
                    //绘制长度边界
                    size_str1 = hv_DetectLeadsStri.TupleSelect(4);
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                    }
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_ObjectSelectedRect, HDevWindowStack.GetActive()
                            );
                    }
                    HOperatorSet.DispLine(200000, hv_RowEdgePos, hv_ColumnEdgePos - 50, hv_RowEdgePos,
                        hv_ColumnEdgePos + 50);

                    //器件四边有引脚
                }
                else if ((int)((new HTuple(hv_LeadDivisionValue.TupleGreaterEqual(
                    0.95))).TupleAnd(new HTuple(hv_LeadDivisionValue.TupleLess(1.2)))) != 0)
                {
                    //正方形四边有引脚
                    if ((int)((new HTuple(hv_BodyRatio.TupleGreaterEqual(0.9))).TupleAnd(new HTuple(hv_BodyRatio.TupleLess(
                        1.2)))) != 0)
                    {
                        //set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.DispObj(ho_ImageAffinTransIC, HDevWindowStack.GetActive()
                                );
                        }
                        //disp_message (200000, DetectLeadsStri[1], 'ImageAffinTrans', RowPartIC-DistancePos1Max*0.35, ColumnPartIC, 'green', 'false')

                        hv_RowEdgePos = hv_RowEdgePos1.Clone();
                        hv_ColumnEdgePos = hv_ColumnEdgePos1.Clone();
                        hv_DistancePos = hv_DistancePos1.Clone();
                        //绘制长度边界
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                        }
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.DispObj(ho_EmptyObjectRect, HDevWindowStack.GetActive()
                                );
                        }
                        size_str1 = hv_DetectLeadsStri.TupleSelect(1);
                        HOperatorSet.DispLine(200000, hv_RowEdgePos1 - 50, hv_ColumnEdgePos1, hv_RowEdgePos1 + 50,
                            hv_ColumnEdgePos1);
                        HOperatorSet.DispLine(200000, hv_RowEdgePos2, hv_ColumnEdgePos2 - 50, hv_RowEdgePos2,
                            hv_ColumnEdgePos2 + 50);

                        //长方形四边有引脚
                    }
                    else if ((int)(new HTuple(hv_BodyRatio.TupleGreaterEqual(1.2))) != 0)
                    {

                        //set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.DispObj(ho_ImageAffinTransIC, HDevWindowStack.GetActive()
                                );
                        }
                        //disp_message (200000, DetectLeadsStri[2], 'ImageAffinTrans', RowPartIC-DistancePos2Max*0.35, ColumnPartIC, 'green', 'false')
                        size_str1 = hv_DetectLeadsStri.TupleSelect(2);
                        hv_RowEdgePos = hv_RowEdgePos1.Clone();
                        hv_ColumnEdgePos = hv_ColumnEdgePos1.Clone();
                        hv_DistancePos = hv_DistancePos1.Clone();

                        //绘制长度边界
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                        }
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.DispObj(ho_EmptyObjectRect, HDevWindowStack.GetActive()
                                );
                        }
                        HOperatorSet.DispLine(200000, hv_RowEdgePos1 - 50, hv_ColumnEdgePos1, hv_RowEdgePos1 + 50,
                            hv_ColumnEdgePos1);
                        HOperatorSet.DispLine(200000, hv_RowEdgePos2, hv_ColumnEdgePos2 - 50, hv_RowEdgePos2,
                            hv_ColumnEdgePos2 + 50);
                    }
                }

                //以文字形式输出长轴方向长度信息
                if ((int)(new HTuple((new HTuple(hv_DistancePos.TupleLength())).TupleGreater(
                    3))) != 0)
                {
                    HOperatorSet.TupleSort(hv_DistancePos, out hv_SortedDistance);
                    HOperatorSet.TupleSortIndex(hv_DistancePos, out hv_IndicesDistance);
                    hv_i1 = (new HTuple(hv_IndicesDistance.TupleLength())) - 3;
                    hv_i2 = (new HTuple(hv_IndicesDistance.TupleLength())) - 1;
                    hv_IndicesDistance = hv_IndicesDistance.TupleSelectRange(hv_i1, hv_i2);
                    HOperatorSet.TupleSort(hv_IndicesDistance, out hv_Sorted);
                    hv_DistancePos = ((((hv_DistancePos.TupleSelect(hv_Sorted.TupleSelect(0)))).TupleConcat(
                        hv_DistancePos.TupleSelect(hv_Sorted.TupleSelect(1))))).TupleConcat(
                        hv_DistancePos.TupleSelect(hv_Sorted.TupleSelect(2)));

                    hv_LeftPin = hv_DistancePos.TupleSelect(0);
                    hv_RightPin = hv_DistancePos.TupleSelect(2);
                    hv_MeanPinLength = ((hv_DistancePos.TupleSelect(0)) + (hv_DistancePos.TupleSelect(
                        2))) * 0.5;

                    for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_DistancePos.TupleLength())) - 1); hv_i = (int)hv_i + 1)
                    {
                        hv_string = new HTuple();
                        hv_string[0] = "左侧引脚长度：";
                        hv_string[1] = "芯片封装宽度：";
                        hv_string[2] = "右侧引脚长度：";
                        //set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
                        //disp_message (200000, string[i]+DistancePos[i]$'.2f', 'ImageAffinTrans', RowEdgePos[0]+i*50, ColumnEdgePos[0]+200, 'yellow', 'false')
                        size_str2 += "\r\n\r\n" + (hv_string.TupleSelect(hv_i)) + (((hv_DistancePos.TupleSelect(hv_i))).TupleString(".2f") + "mm");

                    }
                }
                else if ((int)(new HTuple((new HTuple(hv_DistancePos.TupleLength()
                    )).TupleEqual(3))) != 0)
                {
                    hv_LeftPin = hv_DistancePos.TupleSelect(0);
                    hv_RightPin = hv_DistancePos.TupleSelect(2);
                    hv_MeanPinLength = ((hv_DistancePos.TupleSelect(0)) + (hv_DistancePos.TupleSelect(
                        2))) * 0.5;
                    for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_DistancePos.TupleLength())) - 1); hv_i = (int)hv_i + 1)
                    {
                        hv_string = new HTuple();
                        hv_string[0] = "左侧引脚长度：";
                        hv_string[1] = "芯片封装宽度：";
                        hv_string[2] = "右侧引脚长度：";
                        //set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
                        //disp_message (200000, string[i]+DistancePos[i]$'.2f', 'ImageAffinTrans', RowEdgePos1[0]+i*50, ColumnEdgePos1[0]+200, 'yellow', 'false')
                        size_str2 += "\r\n\r\n" + (hv_string.TupleSelect(hv_i)) + (((hv_DistancePos.TupleSelect(hv_i))).TupleString(".2f") + "mm");

                    }
                }
                else
                {
                    //set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_ImageAffinTransIC, HDevWindowStack.GetActive()
                            );
                    }
                    //disp_message (200000, '未检测到引脚', 'ImageAffinTrans', RowPartIC, ColumnPartIC, 'red', 'false')
                    // stop(...); only in hdevelop
                    size_str2 = "\r\n\r\n" + "未检测到引脚！";
                }


                // stop(...); only in hdevelop

                //测量引脚数量、宽度、间距
                if (size_str1 != "器件无引脚")
                {
                    hv_Phi = new HTuple();
                    hv_Phi = hv_Phi.TupleConcat((new HTuple(-90)).TupleRad()
                        );
                    hv_Phi = hv_Phi.TupleConcat((new HTuple(0)).TupleRad());
                    hv_Phi = hv_Phi.TupleConcat((new HTuple(-90)).TupleRad()
                        );
                    hv_Phi = hv_Phi.TupleConcat((new HTuple(0)).TupleRad());
                    hv_RowMeasureRec = new HTuple();
                    hv_RowMeasureRec = hv_RowMeasureRec.TupleConcat(hv_RowPartIC);
                    hv_RowMeasureRec = hv_RowMeasureRec.TupleConcat((hv_RowPartIC - (hv_DistancePos2Max * 0.5)) - (hv_MeanPinLength * 0.8));
                    hv_RowMeasureRec = hv_RowMeasureRec.TupleConcat(hv_RowPartIC);
                    hv_RowMeasureRec = hv_RowMeasureRec.TupleConcat((hv_RowPartIC + (hv_DistancePos2Max * 0.5)) + (hv_MeanPinLength * 0.8));
                    hv_ColuMeasureRec = new HTuple();
                    hv_ColuMeasureRec = hv_ColuMeasureRec.TupleConcat((hv_ColumnPartIC + (hv_DistancePos1Max * 0.5)) + (hv_MeanPinLength * 0.8));
                    hv_ColuMeasureRec = hv_ColuMeasureRec.TupleConcat(hv_ColumnPartIC);
                    hv_ColuMeasureRec = hv_ColuMeasureRec.TupleConcat((hv_ColumnPartIC - (hv_DistancePos1Max * 0.5)) - (hv_MeanPinLength * 0.8));
                    hv_ColuMeasureRec = hv_ColuMeasureRec.TupleConcat(hv_ColumnPartIC);
                    hv_Length1_Pairs = (hv_DistancePos1Max * 0.5) + 10;
                    hv_Length2_Pairs = (hv_DistancePos2Max * 0.5) + 10;
                    hv_Length_Pairs = new HTuple();
                    hv_Length_Pairs = hv_Length_Pairs.TupleConcat(hv_Length2_Pairs);
                    hv_Length_Pairs = hv_Length_Pairs.TupleConcat(hv_Length1_Pairs);
                    hv_Length_Pairs = hv_Length_Pairs.TupleConcat(hv_Length2_Pairs);
                    hv_Length_Pairs = hv_Length_Pairs.TupleConcat(hv_Length1_Pairs);
                    hv_RectWidth = 30;
                    //确定灰度测量矩形
                    ho_RectangleLeads1.Dispose();
                    HOperatorSet.GenRectangle2(out ho_RectangleLeads1, hv_RowMeasureRec.TupleSelect(
                        0), hv_ColuMeasureRec.TupleSelect(0), hv_Phi.TupleSelect(0), hv_Length_Pairs.TupleSelect(
                        0), hv_RectWidth);
                    ho_RectangleLeads2.Dispose();
                    HOperatorSet.GenRectangle2(out ho_RectangleLeads2, hv_RowMeasureRec.TupleSelect(
                        1), hv_ColuMeasureRec.TupleSelect(1), hv_Phi.TupleSelect(1), hv_Length_Pairs.TupleSelect(
                        1), hv_RectWidth);
                    ho_RectangleLeads3.Dispose();
                    HOperatorSet.GenRectangle2(out ho_RectangleLeads3, hv_RowMeasureRec.TupleSelect(
                        2), hv_ColuMeasureRec.TupleSelect(2), hv_Phi.TupleSelect(2), hv_Length_Pairs.TupleSelect(
                        2), hv_RectWidth);
                    ho_RectangleLeads4.Dispose();
                    HOperatorSet.GenRectangle2(out ho_RectangleLeads4, hv_RowMeasureRec.TupleSelect(
                        3), hv_ColuMeasureRec.TupleSelect(3), hv_Phi.TupleSelect(3), hv_Length_Pairs.TupleSelect(
                        3), hv_RectWidth);

                    ho_RectangleLeads.Dispose();
                    HOperatorSet.GenEmptyObj(out ho_RectangleLeads);

                    if ((int)(new HTuple(hv_LeadDivisionValue.TupleGreater(10))) != 0)
                    {
                        hv_Phi = new HTuple();
                        hv_Phi = hv_Phi.TupleConcat((new HTuple(-90)).TupleRad()
                            );
                        hv_Phi = hv_Phi.TupleConcat((new HTuple(-90)).TupleRad()
                            );
                        hv_RowMeasureRec = new HTuple();
                        hv_RowMeasureRec = hv_RowMeasureRec.TupleConcat(hv_RowPartIC);
                        hv_RowMeasureRec = hv_RowMeasureRec.TupleConcat(hv_RowPartIC);
                        hv_ColuMeasureRec = new HTuple();
                        hv_ColuMeasureRec = hv_ColuMeasureRec.TupleConcat((hv_ColumnPartIC + (hv_DistancePos1Max * 0.5)) + (hv_MeanPinLength * 0.8));
                        hv_ColuMeasureRec = hv_ColuMeasureRec.TupleConcat((hv_ColumnPartIC - (hv_DistancePos1Max * 0.5)) - (hv_MeanPinLength * 0.8));
                        hv_Length_Pairs = new HTuple();
                        hv_Length_Pairs = hv_Length_Pairs.TupleConcat(hv_Length2_Pairs);
                        hv_Length_Pairs = hv_Length_Pairs.TupleConcat(hv_Length2_Pairs);
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.ConcatObj(ho_RectangleLeads, ho_RectangleLeads1, out ExpTmpOutVar_0
                                );
                            ho_RectangleLeads.Dispose();
                            ho_RectangleLeads = ExpTmpOutVar_0;
                        }

                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.ConcatObj(ho_RectangleLeads, ho_RectangleLeads3, out ExpTmpOutVar_0
                                );
                            ho_RectangleLeads.Dispose();
                            ho_RectangleLeads = ExpTmpOutVar_0;
                        }

                    }
                    else if ((int)(new HTuple(hv_LeadDivisionValue.TupleLess(0.95))) != 0)
                    {
                        hv_Phi = new HTuple();
                        hv_Phi = hv_Phi.TupleConcat((new HTuple(0)).TupleRad()
                            );
                        hv_Phi = hv_Phi.TupleConcat((new HTuple(0)).TupleRad()
                            );
                        hv_RowMeasureRec = new HTuple();
                        hv_RowMeasureRec = hv_RowMeasureRec.TupleConcat((hv_RowPartIC - (hv_DistancePos2Max * 0.5)) - (hv_MeanPinLength * 0.8));
                        hv_RowMeasureRec = hv_RowMeasureRec.TupleConcat((hv_RowPartIC + (hv_DistancePos2Max * 0.5)) + (hv_MeanPinLength * 0.8));
                        hv_ColuMeasureRec = new HTuple();
                        hv_ColuMeasureRec = hv_ColuMeasureRec.TupleConcat(hv_ColumnPartIC);
                        hv_ColuMeasureRec = hv_ColuMeasureRec.TupleConcat(hv_ColumnPartIC);
                        hv_Length_Pairs = new HTuple();
                        hv_Length_Pairs = hv_Length_Pairs.TupleConcat(hv_Length1_Pairs);
                        hv_Length_Pairs = hv_Length_Pairs.TupleConcat(hv_Length1_Pairs);

                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.ConcatObj(ho_RectangleLeads, ho_RectangleLeads2, out ExpTmpOutVar_0
                                );
                            ho_RectangleLeads.Dispose();
                            ho_RectangleLeads = ExpTmpOutVar_0;
                        }

                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.ConcatObj(ho_RectangleLeads, ho_RectangleLeads4, out ExpTmpOutVar_0
                                );
                            ho_RectangleLeads.Dispose();
                            ho_RectangleLeads = ExpTmpOutVar_0;
                        }
                    }
                    else if ((int)((new HTuple(hv_LeadDivisionValue.TupleGreaterEqual(
                        0.95))).TupleAnd(new HTuple(hv_LeadDivisionValue.TupleLess(1.2)))) != 0)
                    {
                        hv_Phi = new HTuple();
                        hv_Phi = hv_Phi.TupleConcat((new HTuple(-90)).TupleRad()
                            );
                        hv_Phi = hv_Phi.TupleConcat((new HTuple(0)).TupleRad()
                            );
                        hv_Phi = hv_Phi.TupleConcat((new HTuple(-90)).TupleRad()
                            );
                        hv_Phi = hv_Phi.TupleConcat((new HTuple(0)).TupleRad()
                            );
                        hv_RowMeasureRec = new HTuple();
                        hv_RowMeasureRec = hv_RowMeasureRec.TupleConcat(hv_RowPartIC);
                        hv_RowMeasureRec = hv_RowMeasureRec.TupleConcat((hv_RowPartIC - (hv_DistancePos2Max * 0.5)) - (hv_MeanPinLength * 0.8));
                        hv_RowMeasureRec = hv_RowMeasureRec.TupleConcat(hv_RowPartIC);
                        hv_RowMeasureRec = hv_RowMeasureRec.TupleConcat((hv_RowPartIC + (hv_DistancePos2Max * 0.5)) + (hv_MeanPinLength * 0.8));
                        hv_ColuMeasureRec = new HTuple();
                        hv_ColuMeasureRec = hv_ColuMeasureRec.TupleConcat((hv_ColumnPartIC + (hv_DistancePos1Max * 0.5)) + (hv_MeanPinLength * 0.8));
                        hv_ColuMeasureRec = hv_ColuMeasureRec.TupleConcat(hv_ColumnPartIC);
                        hv_ColuMeasureRec = hv_ColuMeasureRec.TupleConcat((hv_ColumnPartIC - (hv_DistancePos1Max * 0.5)) - (hv_MeanPinLength * 0.8));
                        hv_ColuMeasureRec = hv_ColuMeasureRec.TupleConcat(hv_ColumnPartIC);
                        hv_Length_Pairs = new HTuple();
                        hv_Length_Pairs = hv_Length_Pairs.TupleConcat(hv_Length2_Pairs);
                        hv_Length_Pairs = hv_Length_Pairs.TupleConcat(hv_Length1_Pairs);
                        hv_Length_Pairs = hv_Length_Pairs.TupleConcat(hv_Length2_Pairs);
                        hv_Length_Pairs = hv_Length_Pairs.TupleConcat(hv_Length1_Pairs);
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.ConcatObj(ho_RectangleLeads, ho_RectangleLeads1, out ExpTmpOutVar_0
                                );
                            ho_RectangleLeads.Dispose();
                            ho_RectangleLeads = ExpTmpOutVar_0;
                        }
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.ConcatObj(ho_RectangleLeads, ho_RectangleLeads2, out ExpTmpOutVar_0
                                );
                            ho_RectangleLeads.Dispose();
                            ho_RectangleLeads = ExpTmpOutVar_0;
                        }
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.ConcatObj(ho_RectangleLeads, ho_RectangleLeads3, out ExpTmpOutVar_0
                                );
                            ho_RectangleLeads.Dispose();
                            ho_RectangleLeads = ExpTmpOutVar_0;
                        }
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.ConcatObj(ho_RectangleLeads, ho_RectangleLeads4, out ExpTmpOutVar_0
                                );
                            ho_RectangleLeads.Dispose();
                            ho_RectangleLeads = ExpTmpOutVar_0;
                        }
                    }

                    hv_j = "①： ";
                    hv_j[1] = "②： ";
                    hv_j[2] = "③： ";
                    hv_j[3] = "④： ";
                    for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_Phi.TupleLength())) - 1); hv_i = (int)hv_i + 1)
                    {

                        //灰度测量

                        HOperatorSet.GenMeasureRectangle2(hv_RowMeasureRec.TupleSelect(hv_i), hv_ColuMeasureRec.TupleSelect(
                            hv_i), hv_Phi.TupleSelect(hv_i), hv_Length_Pairs.TupleSelect(hv_i),
                            hv_RectWidth, hv_WidthIC, hv_HeightIC, "nearest_neighbor", out hv_MeasureHandlePairs1);

                        //计算引脚宽度及间隙
                        ho_ObjectSelected.Dispose();
                        HOperatorSet.SelectObj(ho_RectangleLeads, out ho_ObjectSelected, hv_i + 1);
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.DispObj(ho_ImageAffinTransIC, HDevWindowStack.GetActive()
                                );
                        }
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                        }
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.DispObj(ho_ObjectSelected, HDevWindowStack.GetActive());
                        }

                        //镀金芯片
                        HOperatorSet.MeasurePairs(ho_ImageAffinTransIC, hv_MeasureHandlePairs1,
                            1, 20, "all", "all", out hv_RowEdgeFirst, out hv_ColumnEdgeFirst, out hv_AmplitudeFirst,
                            out hv_RowEdgeSecond, out hv_ColumnEdgeSecond, out hv_AmplitudeSecond,
                            out hv_IntraDistance, out hv_InterDistance);
                        //非镀金芯片
                        //measure_pairs (ImageAffinTrans, MeasureHandle, 1, 20, 'all', 'all', RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)

                        //tuple_min (abs(AmplitudeFirst), MinFirst)
                        //tuple_min (AmplitudeSecond, MinSecond)
                        //tuple_min2 (MinFirst, MinSecond, Min2)
                        HOperatorSet.CloseMeasure(hv_MeasureHandlePairs1);

                        HOperatorSet.DispLine(hv_WindowHandle, hv_RowEdgeFirst, hv_ColumnEdgeFirst,
                            hv_RowEdgeSecond, hv_ColumnEdgeSecond);
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                        }
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1);
                        }
                        HOperatorSet.DispLine(hv_WindowHandle, hv_RowEdgeFirst - (20 * (((hv_Phi.TupleSelect(
                            hv_i))).TupleCos())), hv_ColumnEdgeFirst - (20 * (((hv_Phi.TupleSelect(
                            hv_i))).TupleSin())), hv_RowEdgeFirst + (20 * (((hv_Phi.TupleSelect(hv_i))).TupleCos()
                            )), hv_ColumnEdgeFirst + (20 * (((hv_Phi.TupleSelect(hv_i))).TupleSin()
                            )));
                        HOperatorSet.DispLine(hv_WindowHandle, hv_RowEdgeSecond - (20 * (((hv_Phi.TupleSelect(
                            hv_i))).TupleCos())), hv_ColumnEdgeSecond - (20 * (((hv_Phi.TupleSelect(
                            hv_i))).TupleSin())), hv_RowEdgeSecond + (20 * (((hv_Phi.TupleSelect(hv_i))).TupleCos()
                            )), hv_ColumnEdgeSecond + (20 * (((hv_Phi.TupleSelect(hv_i))).TupleSin()
                            )));

                        if ((int)(new HTuple(hv_Phi.TupleLength())) == 2 && (int)hv_i == 1 && size_str1 == "长轴方向两边有引脚")
                            hv_j[1] = hv_j[2];
                        if ((int)(new HTuple(hv_Phi.TupleLength())) == 2 && size_str1 == "短轴方向两边有引脚")
                        {
                            hv_j[0] = hv_j[1];
                            hv_j[1] = hv_j[3];
                        }

                        hv_MeasureResult = hv_j.TupleSelect(hv_i) + "芯片引脚数量：";
                        if (hv_MeasureResult == null)
                            hv_MeasureResult = new HTuple();
                        hv_MeasureResult[1] = "芯片引脚宽度：";
                        if (hv_MeasureResult == null)
                            hv_MeasureResult = new HTuple();
                        hv_MeasureResult[2] = "引脚间距：";
                        //set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
                        //disp_message (WindowHandle, MeasureResult[0]+|IntraDistance|, 'ImageAffinTrans', RowPartIC, ColumnPartIC, 'yellow', 'false')
                        //disp_message (WindowHandle, MeasureResult[1]+mean(InterDistance)$'.2f', 'ImageAffinTrans', RowPartIC+50, ColumnPartIC, 'yellow', 'false')
                        //disp_message (WindowHandle, MeasureResult[2]+(mean(InterDistance)*2)$'.2f', 'ImageAffinTrans', RowPartIC+100, ColumnPartIC, 'yellow', 'false')
                        // stop(...); only in hdevelop
                        size_str3 += "\r\n\r\n" + (hv_MeasureResult.TupleSelect(0)) + (new HTuple(hv_IntraDistance.TupleLength())) + "mm\t  ";
                        size_str3 += (hv_MeasureResult.TupleSelect(1)) + ((new HTuple(hv_InterDistance.TupleMean())).TupleString(".2f")) + "mm\t  ";
                        size_str3 += (hv_MeasureResult.TupleSelect(2)) + ((new HTuple((hv_InterDistance.TupleMean()) * 2)).TupleString(".2f")) + "mm";



                    }
                    //endfor
                    //转换为物理尺寸
                    hv_CameraParameters = new HTuple();
                    hv_CameraParameters[0] = "area_scan_division";
                    hv_CameraParameters[1] = 0.0106344;
                    hv_CameraParameters[2] = -736.862;
                    hv_CameraParameters[3] = 3.45e-006;
                    hv_CameraParameters[4] = 3.45e-006;
                    hv_CameraParameters[5] = 1259.21;
                    hv_CameraParameters[6] = 962.069;
                    hv_CameraParameters[7] = 2448;
                    hv_CameraParameters[8] = 2048;
                    hv_CameraPose = new HTuple();
                    hv_CameraPose[0] = 0.00246144;
                    hv_CameraPose[1] = 0.000896562;
                    hv_CameraPose[2] = 0.0885442;
                    hv_CameraPose[3] = 359.518;
                    hv_CameraPose[4] = 0.106107;
                    hv_CameraPose[5] = 274.279;
                    hv_CameraPose[6] = 0;
                    HOperatorSet.ImagePointsToWorldPlane(hv_CameraParameters, hv_CameraPose, hv_RowEdgePos1,
                        hv_ColumnEdgePos1, 0.001, out hv_Column_World_Pos1, out hv_Row_World_Pos2);

                }
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Image.Dispose();
                ho_GrayImage.Dispose();
                ho_ImageEmphasize.Dispose();
                ho_RegionROI.Dispose();
                ho_RegionErosionROI.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_RegionTrans.Dispose();
                ho_ImageROI.Dispose();
                ho_RegionIC.Dispose();
                ho_RegionDilationIC.Dispose();
                ho_ConnectedRegionsIC.Dispose();
                ho_SelectedRegionsIC.Dispose();
                ho_RectangleIC.Dispose();
                ho_ImageAffinTransROI.Dispose();
                ho_RegionAffineTransIC.Dispose();
                ho_ImageReducedIC.Dispose();
                ho_ImageAffinTransIC.Dispose();
                ho_RegionPartIC.Dispose();
                ho_RectanglePartIC1.Dispose();
                ho_RectanglePartIC2.Dispose();
                ho_EmptyObjectRect.Dispose();
                ho_ObjectSelectedRect.Dispose();
                ho_RectangleLeads1.Dispose();
                ho_RectangleLeads2.Dispose();
                ho_RectangleLeads3.Dispose();
                ho_RectangleLeads4.Dispose();
                ho_RectangleLeads.Dispose();
                ho_ObjectSelected.Dispose();

                throw HDevExpDefaultException;
            }
            ho_Image.Dispose();
            ho_GrayImage.Dispose();
            ho_ImageEmphasize.Dispose();
            ho_RegionROI.Dispose();
            ho_RegionErosionROI.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions2.Dispose();
            ho_RegionTrans.Dispose();
            ho_ImageROI.Dispose();
            ho_RegionIC.Dispose();
            ho_RegionDilationIC.Dispose();
            ho_ConnectedRegionsIC.Dispose();
            ho_SelectedRegionsIC.Dispose();
            ho_RectangleIC.Dispose();
            ho_ImageAffinTransROI.Dispose();
            ho_RegionAffineTransIC.Dispose();
            ho_ImageReducedIC.Dispose();
            //ho_ImageAffinTransIC.Dispose();
            ho_RegionPartIC.Dispose();
            ho_RectanglePartIC1.Dispose();
            ho_RectanglePartIC2.Dispose();
            ho_EmptyObjectRect.Dispose();
            ho_ObjectSelectedRect.Dispose();
            ho_RectangleLeads1.Dispose();
            ho_RectangleLeads2.Dispose();
            ho_RectangleLeads3.Dispose();
            ho_RectangleLeads4.Dispose();
            ho_RectangleLeads.Dispose();
            ho_ObjectSelected.Dispose();

        }


        public HObject TD_image1;
        public HTuple TD_Message;
        public void action1(HObject xx1)
        {


            // Local iconic variables 

            HObject ho_Image, ho_GrayImage, ho_RegionROI;
            HObject ho_RegionErosionROI, ho_ConnectedRegions, ho_SelectedRegions2;
            HObject ho_RegionTrans, ho_ImageROI, ho_RegionIC, ho_RegionDilationIC;
            HObject ho_ConnectedRegionsIC, ho_SelectedRegionsIC, ho_RectangleIC;
            HObject ho_ImageReducedIC, ho_ImagePartIC, ho_RegionPartIC;
            HObject ho_ImageAffinTrans, ho_RegionAffineTrans, ho_RectanglePartIC;
            HObject ho_RectangleBody, ho_RegionLeads, ho_RegionErosion;
            HObject ho_ConnectedRegionsLeads, ho_SelectedRegionsLeads = null;
            HObject ho_RegionUnion = null, ho_ImageResult = null, ho_Image1 = null;

            // Local control variables 

            HTuple hv_Width = null, hv_Height = null, hv_Ratio = null;
            HTuple hv_Max_area = null, hv_WindowHandle = null, hv_Row = null;
            HTuple hv_Column = null, hv_Phi = null, hv_Length1 = null;
            HTuple hv_Length2 = null, hv_WidthIC = null, hv_HeightIC = null;
            HTuple hv_RowPartIC = null, hv_ColumnPartIC = null, hv_Phi_PartIC = null;
            HTuple hv_Length1_PartIC = null, hv_Length2_PartIC = null;
            HTuple hv_HomMat2D = null, hv_Mean = null, hv_Deviation = null;
            HTuple hv_MeasureHandle = null, hv_RowEdge = null, hv_ColumnEdge = null;
            HTuple hv_Amplitude = null, hv_Distance = null, hv_Body = null;
            HTuple hv_Area = null, hv_Row1 = null, hv_Column1 = null;
            HTuple hv_Median = null, hv_shorter = null, hv_IndexShorter = null;
            HTuple hv_ShorterSum = null, hv_IndexArea = null, hv_Shorter = new HTuple();
            HTuple hv_MaxArea = new HTuple(), hv_MinArea = new HTuple();
            HTuple hv_Message = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_RegionROI);
            HOperatorSet.GenEmptyObj(out ho_RegionErosionROI);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_RegionTrans);
            HOperatorSet.GenEmptyObj(out ho_ImageROI);
            HOperatorSet.GenEmptyObj(out ho_RegionIC);
            HOperatorSet.GenEmptyObj(out ho_RegionDilationIC);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsIC);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsIC);
            HOperatorSet.GenEmptyObj(out ho_RectangleIC);
            HOperatorSet.GenEmptyObj(out ho_ImageReducedIC);
            HOperatorSet.GenEmptyObj(out ho_ImagePartIC);
            HOperatorSet.GenEmptyObj(out ho_RegionPartIC);
            HOperatorSet.GenEmptyObj(out ho_ImageAffinTrans);
            HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans);
            HOperatorSet.GenEmptyObj(out ho_RectanglePartIC);
            HOperatorSet.GenEmptyObj(out ho_RectangleBody);
            HOperatorSet.GenEmptyObj(out ho_RegionLeads);
            HOperatorSet.GenEmptyObj(out ho_RegionErosion);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsLeads);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsLeads);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion);
            HOperatorSet.GenEmptyObj(out ho_ImageResult);
            HOperatorSet.GenEmptyObj(out ho_Image1);
            try
            {
                // dev_update_window(...); only in hdevelop
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.CloseWindow(HDevWindowStack.Pop());
                }
                ho_Image.Dispose();
                ho_Image = xx1;
                //HOperatorSet.ReadImage(out ho_Image, "C:/Users/于立昆/Desktop/image2/4");
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                hv_Ratio = 0.25;
                hv_Max_area = hv_Width * hv_Height;

                //dev_open_window (0, 0, Width*Ratio, Height*Ratio, 'black', WindowHandle)
                if (HDevWindowStack.IsOpen())
                {
                    //dev_display (Image)
                }
                //*选出亮区域
                ho_GrayImage.Dispose();
                HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
                ho_RegionROI.Dispose();
                HOperatorSet.Threshold(ho_GrayImage, out ho_RegionROI, 220, 255);
                ho_RegionErosionROI.Dispose();
                HOperatorSet.ErosionCircle(ho_RegionROI, out ho_RegionErosionROI, 25);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_RegionErosionROI, out ho_ConnectedRegions);
                ho_SelectedRegions2.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions2, "max_area",
                    70);
                ho_RegionTrans.Dispose();
                HOperatorSet.ShapeTrans(ho_SelectedRegions2, out ho_RegionTrans, "convex");
                ho_ImageROI.Dispose();
                HOperatorSet.ReduceDomain(ho_GrayImage, ho_RegionTrans, out ho_ImageROI);
                //选出暗区域即芯片区域
                ho_RegionIC.Dispose();
                HOperatorSet.Threshold(ho_ImageROI, out ho_RegionIC, 0, 240);
                ho_RegionDilationIC.Dispose();
                HOperatorSet.DilationRectangle1(ho_RegionIC, out ho_RegionDilationIC, 3, 3);
                ho_ConnectedRegionsIC.Dispose();
                HOperatorSet.Connection(ho_RegionDilationIC, out ho_ConnectedRegionsIC);

                ho_SelectedRegionsIC.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegionsIC, out ho_SelectedRegionsIC,
                    "max_area", 70);
                //画出芯片最小外接矩形
                HOperatorSet.SmallestRectangle2(ho_SelectedRegionsIC, out hv_Row, out hv_Column,
                    out hv_Phi, out hv_Length1, out hv_Length2);
                ho_RectangleIC.Dispose();
                HOperatorSet.GenRectangle2(out ho_RectangleIC, hv_Row, hv_Column, hv_Phi, hv_Length1,
                    hv_Length2);
                ho_RegionDilationIC.Dispose();
                HOperatorSet.DilationRectangle1(ho_RectangleIC, out ho_RegionDilationIC, 10,
                    10);

                ho_ImageReducedIC.Dispose();
                HOperatorSet.ReduceDomain(ho_ImageROI, ho_RegionDilationIC, out ho_ImageReducedIC
                    );
                ho_ImagePartIC.Dispose();
                HOperatorSet.CropDomain(ho_ImageReducedIC, out ho_ImagePartIC);

                if (HDevWindowStack.IsOpen())
                {
                    //dev_display (ImagePartIC)
                }
                HOperatorSet.GetImageSize(ho_ImagePartIC, out hv_WidthIC, out hv_HeightIC);


                HOperatorSet.SetWindowAttr("background_color", "black");
                HOperatorSet.OpenWindow(0, 0, hv_WidthIC, hv_HeightIC, 0, "invisible", "", out hv_WindowHandle);
                HDevWindowStack.Push(hv_WindowHandle);
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_WidthIC, hv_HeightIC);
                }


                //对裁剪出的芯片处理-只画出芯片区域
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin");
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                }
                ho_RegionPartIC.Dispose();
                HOperatorSet.Threshold(ho_ImagePartIC, out ho_RegionPartIC, 0, 200);
                HOperatorSet.SmallestRectangle2(ho_RegionPartIC, out hv_RowPartIC, out hv_ColumnPartIC,
                    out hv_Phi_PartIC, out hv_Length1_PartIC, out hv_Length2_PartIC);

                //仿射变换
                HOperatorSet.VectorAngleToRigid(hv_RowPartIC, hv_ColumnPartIC, hv_Phi_PartIC,
                    hv_RowPartIC, hv_ColumnPartIC, (new HTuple(0)).TupleRad(), out hv_HomMat2D);
                ho_ImageAffinTrans.Dispose();
                HOperatorSet.AffineTransImage(ho_ImagePartIC, out ho_ImageAffinTrans, hv_HomMat2D,
                    "constant", "false");
                ho_RegionAffineTrans.Dispose();
                HOperatorSet.AffineTransRegion(ho_RegionPartIC, out ho_RegionAffineTrans, hv_HomMat2D,
                    "nearest_neighbor");

                //获得测量矩形
                ho_RectanglePartIC.Dispose();
                HOperatorSet.GenRectangle2(out ho_RectanglePartIC, hv_RowPartIC, hv_ColumnPartIC,
                    0, hv_Length1_PartIC + 5, hv_Length2_PartIC * 0.5);
                HOperatorSet.Intensity(ho_RectanglePartIC, ho_ImageAffinTrans, out hv_Mean,
                    out hv_Deviation);
                //获得测量句柄
                HOperatorSet.GenMeasureRectangle2(hv_RowPartIC, hv_ColumnPartIC, 0, hv_Length1_PartIC + 5,
                    20, hv_WidthIC, hv_HeightIC, "nearest_neighbor", out hv_MeasureHandle);
                //测量芯片宽度引脚长度

                HOperatorSet.MeasurePos(ho_ImageAffinTrans, hv_MeasureHandle, 1, 30, "all",
                    "all", out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude, out hv_Distance);
                HOperatorSet.TupleMax(hv_Distance, out hv_Body);
                //显示测量结果
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                }
                if (HDevWindowStack.IsOpen())
                {
                    //dev_display (ImageAffinTrans)
                }

                //disp_line (200000, RowEdge-100, ColumnEdge, RowEdge+100, ColumnEdge)
                if ((int)(new HTuple((new HTuple(hv_Distance.TupleLength())).TupleLessEqual(
                    3))) != 0)
                {
                    //tuple_sort (Distance, SortedDistance)
                    //tuple_sort_index (Distance, IndicesDistance)
                    //tuple_remove (Distance, IndicesDistance[0:|Distance|-4], NewDistance)

                    //for i := 0 to |NewDistance|-1 by 1
                    //set_display_font (WindowHandle, Body/40, 'mono', 'true', 'false')
                    //string := ['左侧引脚长度：','芯片封装宽度：','右侧引脚长度：']
                    //disp_message (200000, string[i]+NewDistance[i]$'.2f', 'ImagePart', RowEdge[0]+i*(Body/6), ColumnEdge[0]+Body/6, 'yellow', 'false')
                    //endfor
                    //*     elseif (|Distance|=3)
                    //for i := 0 to |Distance|-1 by 1
                    //set_display_font (WindowHandle, Body/40, 'mono', 'true', 'false')
                    //string := ['左侧引脚长度：','芯片封装宽度：','右侧引脚长度：']
                    //disp_message (200000, string[i]+Distance[i]$'.2f', 'ImageAffinTrans', RowEdge[0]+i*(Body/6), ColumnEdge[0]+Body/6, 'yellow', 'false')
                    //endfor
                    //*     else
                    //set_display_font (WindowHandle, Body/40, 'mono', 'true', 'false')
                    //disp_message (200000, '未检测到引脚', 'ImageAffinTrans', RowPartIC, ColumnPartIC-2.5*(Body/40), 'red', 'false')
                }

                HOperatorSet.CloseMeasure(hv_MeasureHandle);
                //确定封装本体矩形

                ho_RectangleBody.Dispose();
                HOperatorSet.GenRectangle2(out ho_RectangleBody, hv_RowPartIC, hv_ColumnPartIC,
                    0, (hv_Body * 0.5) + 10, (hv_Body * 0.5) + 10);
                //去掉封装本体
                ho_RegionLeads.Dispose();
                HOperatorSet.Difference(ho_RegionAffineTrans, ho_RectangleBody, out ho_RegionLeads
                    );
                ho_RegionErosion.Dispose();
                HOperatorSet.ErosionRectangle1(ho_RegionLeads, out ho_RegionErosion, 2.5, 2.5);
                ho_ConnectedRegionsLeads.Dispose();
                HOperatorSet.Connection(ho_RegionErosion, out ho_ConnectedRegionsLeads);
                //计算每一个连通域的面积
                HOperatorSet.AreaCenter(ho_ConnectedRegionsLeads, out hv_Area, out hv_Row1,
                    out hv_Column1);
                HOperatorSet.TupleMedian(hv_Area, out hv_Median);

                hv_shorter = new HTuple();
                hv_IndexShorter = 0;
                hv_ShorterSum = 0;
                for (hv_IndexArea = 0; (int)hv_IndexArea <= (int)((new HTuple(hv_Area.TupleLength()
                    )) - 1); hv_IndexArea = (int)hv_IndexArea + 1)
                {
                    if ((int)(new HTuple(((hv_Area.TupleSelect(hv_IndexArea))).TupleGreater(hv_Median * 1.5))) != 0)
                    {
                        hv_ShorterSum = hv_ShorterSum + 1;
                        if (hv_Shorter == null)
                            hv_Shorter = new HTuple();
                        hv_Shorter[hv_IndexShorter] = hv_Area.TupleSelect(hv_IndexArea);
                        hv_IndexShorter = hv_IndexShorter + 1;
                        HOperatorSet.TupleMax(hv_Shorter, out hv_MaxArea);
                        HOperatorSet.TupleMin(hv_Shorter, out hv_MinArea);
                        ho_SelectedRegionsLeads.Dispose();
                        HOperatorSet.SelectShape(ho_ConnectedRegionsLeads, out ho_SelectedRegionsLeads,
                            "area", "and", hv_MinArea, hv_MaxArea);
                    }
                }


                if (HDevWindowStack.IsOpen())
                {
                    //dev_clear_window ()
                }
                if (HDevWindowStack.IsOpen())
                {
                    //dev_display (ImageAffinTrans)
                }

                if ((int)(new HTuple(hv_ShorterSum.TupleEqual(0))) != 0)
                {
                    //set_display_font (WindowHandle, Body/20, 'mono', 'true', 'false')
                    //disp_message (200000, '合 格', 'ImageAffinTrans', RowPartIC-Body/20, ColumnPartIC-Body/8, 'green', 'false')
                    TD_image1 = ho_ImageAffinTrans;
                    TD_Message = "合 格";
                }
                else
                {
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin");
                    }
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                    }
                    ho_RegionUnion.Dispose();
                    HOperatorSet.Union1(ho_SelectedRegionsLeads, out ho_RegionUnion);

                    ho_ImageResult.Dispose();
                    HOperatorSet.PaintRegion(ho_RegionUnion, ho_ImageAffinTrans, out ho_ImageResult,
                        255, "fill");
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_ImageAffinTrans, HDevWindowStack.GetActive());
                    }
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_RegionUnion, HDevWindowStack.GetActive());
                    }
                    if (HDevWindowStack.IsOpen())
                    {
                        //dev_display (ImageResult)
                    }



                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_WidthIC, hv_HeightIC);
                    }
                    ho_Image1.Dispose();
                    HOperatorSet.DumpWindowImage(out ho_Image1, hv_WindowHandle);
                    //*     get_image_size (Image1, Width1, Height1)
                    TD_image1 = ho_Image1;
                    HOperatorSet.WriteImage(TD_image1, "bmp", 0, "C:/Users/于立昆/Desktop/image/2");

                    hv_Message = "不 合 格";
                    //disp_message (200000, Message, 'ImageAffinTrans', RowPartIC-Body/8, ColumnPartIC-Body/8, 'red', 'false')
                    //set_display_font (WindowHandle, Body/60, 'mono', 'true', 'false')
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                    }
                    if (hv_Message == null)
                        hv_Message = new HTuple();
                    hv_Message[1] = "共 有：";
                    if (hv_Message == null)
                        hv_Message = new HTuple();
                    hv_Message[2] = " 处短路";
                    //color := 'green'
                    //disp_message (200000, Message[1]+ShorterSum+Message[2], 'ImageAffinTrans', RowPartIC+Body/8, ColumnPartIC-Body/8, color, 'false')
                    TD_Message = hv_Message + "\r\n" + ((hv_Message.TupleSelect(1)) + hv_ShorterSum) + (hv_Message.TupleSelect(2));
                }




            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Image.Dispose();
                ho_GrayImage.Dispose();
                ho_RegionROI.Dispose();
                ho_RegionErosionROI.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions2.Dispose();
                ho_RegionTrans.Dispose();
                ho_ImageROI.Dispose();
                ho_RegionIC.Dispose();
                ho_RegionDilationIC.Dispose();
                ho_ConnectedRegionsIC.Dispose();
                ho_SelectedRegionsIC.Dispose();
                ho_RectangleIC.Dispose();
                ho_ImageReducedIC.Dispose();
                ho_ImagePartIC.Dispose();
                ho_RegionPartIC.Dispose();
                ho_ImageAffinTrans.Dispose();
                ho_RegionAffineTrans.Dispose();
                ho_RectanglePartIC.Dispose();
                ho_RectangleBody.Dispose();
                ho_RegionLeads.Dispose();
                ho_RegionErosion.Dispose();
                ho_ConnectedRegionsLeads.Dispose();
                ho_SelectedRegionsLeads.Dispose();
                ho_RegionUnion.Dispose();
                ho_ImageResult.Dispose();
                ho_Image1.Dispose();

                throw HDevExpDefaultException;
            }

            ho_Image.Dispose();
            ho_GrayImage.Dispose();
            ho_RegionROI.Dispose();
            ho_RegionErosionROI.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions2.Dispose();
            ho_RegionTrans.Dispose();
            ho_ImageROI.Dispose();
            ho_RegionIC.Dispose();
            ho_RegionDilationIC.Dispose();
            ho_ConnectedRegionsIC.Dispose();
            ho_SelectedRegionsIC.Dispose();
            ho_RectangleIC.Dispose();
            ho_ImageReducedIC.Dispose();
            ho_ImagePartIC.Dispose();
            ho_RegionPartIC.Dispose();
            //ho_ImageAffinTrans.Dispose();
            ho_RegionAffineTrans.Dispose();
            ho_RectanglePartIC.Dispose();
            ho_RectangleBody.Dispose();
            ho_RegionLeads.Dispose();
            ho_RegionErosion.Dispose();
            ho_ConnectedRegionsLeads.Dispose();
            ho_SelectedRegionsLeads.Dispose();
            ho_RegionUnion.Dispose();
            ho_ImageResult.Dispose();
            //ho_Image1.Dispose();
        }
    }
}
